package com.zp.exception;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import com.zp.domain.common.Result;
import com.zp.enums.ErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 *
 * @author zbc
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(com.zp.exception.BusinessException.class)
    public Result<?> businessExceptionHandler(com.zp.exception.BusinessException e) {
        log.error("BusinessException: ", e);
        return com.zp.domain.common.Result.error(e.getCode(), e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public Result<?> runtimeExceptionHandler(RuntimeException e) {
        log.error("RuntimeException: ", e);
        return com.zp.domain.common.Result.error(ErrorCode.SYSTEM_ERROR, "系统错误");
    }

    // 捕获 Sa-Token 抛出的异常
    @ExceptionHandler(NotLoginException.class)
    public Result<?> notLoginException(NotLoginException e) {
        log.error("NotLoginException", e);
        return com.zp.domain.common.Result.error(ErrorCode.NOT_LOGIN_ERROR, e.getMessage());
    }

    // 捕获 Sa-Token 抛出的异常
    @ExceptionHandler(NotPermissionException.class)
    public Result<?> notPermissionExceptionHandler(NotPermissionException e) {
        log.error("NotPermissionException", e);
        return com.zp.domain.common.Result.error(ErrorCode.NO_AUTH_ERROR, e.getMessage());
    }

}